package main

type MovingAverage struct {
	size   int     //滑动窗口设置大小
	sum    float64 //滑动窗口当前含有的元素之和
	window []int   //滑动窗口
}

/** Initialize your data structure here. */
func Constructor(size int) MovingAverage {
	return MovingAverage{
		size:   size,
		sum:    0,
		window: make([]int, 0),
	}
}

func (this *MovingAverage) Next(val int) float64 {
	if len(this.window) >= this.size {
		leftVal := this.window[0]
		this.window = this.window[1:]
		this.sum -= float64(leftVal)
	}
	this.sum += float64(val)
	this.window = append(this.window, val)
	return this.sum / float64(len(this.window))
}

/**
 * Your MovingAverage object will be instantiated and called as such:
 * obj := Constructor(size);
 * param_1 := obj.Next(val);
 */
